﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ORMAC.CodeGen.CodeTemplates;
using Microsoft.VisualStudio.TextTemplating;
using System.IO;

namespace ORMAC.CodeGen
{
    public class CodeGenerator:ICodeGenerator
    {
        #region private members
        /// <summary>
        /// Holds a list of all database objects internaly
        /// </summary>
        List<DBObjectInfo> dbObjects=new List<DBObjectInfo>();
        #endregion
        #region ICodeGenerator Members

        public string GenerateClass(string connectionString, string namespaceName, DBObjectInfo table)
        {
            dbObjects.Add(table);
            if (table is TableInfo)
            {
                TableClassTemplate t = new TableClassTemplate();
                TextTemplatingSession session = new TextTemplatingSession();
                session["NamespaceName"] = namespaceName;
                session["Table"] = table;
                session["classPrefix"] = new Settings().ClassPrefix;
                t.Session = session;
                try
                {
                    t.Initialize();
                    String content = t.TransformText();
                    return content;
                }
                catch (Exception ex)
                {
                    throw;
                };
            }  else if (table is SProcInfo)
            {
                SQLShema schema = new SQLShema();
                TableInfo tInfo= schema.CompleteSprocSchema(connectionString, table as SProcInfo, dbObjects);
                if (tInfo != null)//the stored procedure return class sshould be generated
                    return GenerateClass(connectionString, namespaceName, tInfo);
                else
                    return null;
            }
            else
            {
                return null;
            }
            
        }

        public string GenerateRepository(string connectionString, string namespaceName)
        {
            if (dbObjects == null)
                return string.Empty;
            RepositoryTemplate t = new RepositoryTemplate();
            TextTemplatingSession session = new TextTemplatingSession();
            session["NamespaceName"] = namespaceName;
            var temp = dbObjects.FindAll(d => d is SProcInfo);
            List<SProcInfo> sprocs = temp.Select<DBObjectInfo, SProcInfo>(d => d as SProcInfo).ToList<SProcInfo>();
            temp = dbObjects.FindAll(d => d is TableInfo);
            List<TableInfo> tables = temp.Select<DBObjectInfo, TableInfo>(d => d as TableInfo).ToList<TableInfo>();
            if (sprocs != null)
                session["Sprocs"] = sprocs;
            t.Session = session;
            try
            {
                t.Initialize();
                String content = t.TransformText();
                return content;
            }
            catch (Exception ex)
            {
                throw;
            };
        }

        #endregion
    }
}
